package com.unknowpupil.account.provider.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unknowpupil.account.provider.api.IUnAccountService;
import com.unknowpupil.account.provider.domain.UnAccount;
import com.unknowpupil.account.provider.mapper.UnAccountMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AutoGenerator
 */
@Service(version = "1.0.0")
public class UnAccountServiceImpl extends ServiceImpl<UnAccountMapper, UnAccount>implements IUnAccountService {
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    /**
     * 功能描述: <br>
     * 〈 新增用户 @Resource 线程安全的〉
     *  一个类只做一件事，一个方法只做一种事，初始化和插入时两件事，需要提取方法。
     * @param: account
     * @since: 1.0.0
     * @auther: w10g
     * @return:
     * @Date: 2020/1/4 0:27
     */
    @Override
    public int insert(UnAccount account) {
        //初始化
        initUnAccount(account);
        return  baseMapper.insert(account);
    }
    /**
     * 功能描述: <br>
     * 〈根据用户名查询〉
     *
     * @param: username
     * @since: 1.0.0
     * @auther: w10g
     * @return:  UnAccount {@link UnAccount}
     * @Date: 2020/1/4 1:35
     */
    @Override
    public UnAccount get(String username) {
        return baseMapper.selectOne(new LambdaQueryWrapper<UnAccount>().eq(UnAccount::getUserName, username));
    }

  /**
   * 功能描述: <br>
   * 〈可以根据复杂条件查询〉
   *
   * @param: account {@link UnAccount}
   * @since: 1.0.0
   * @auther: w10g
   * @return:
   * @Date: 2020/1/4 1:36
   */
    @Override
    public UnAccount get(UnAccount account) {
        //可以根据复杂条件查询
        return baseMapper.selectOne(new LambdaQueryWrapper<UnAccount>().eq(UnAccount::getEmail,account.getEmail()));
    }

    /**
     * 功能描述: <br>
     * 〈跟新用户〉
     *
     * @param: [account]
     * @since: 1.0.0
     * @auther: w10g
     * @return: com.unknowpupil.account.provider.domain.UnAccount
     * @Date: 2020/1/4 23:09
     */
    @Override
    public int update(UnAccount account) {
        UnAccount account1 = get(account.getUserName());
        account1.setStatus(0);
        account1.setUserName(account.getUserName());
        account1.setIcon(account.getIcon());
        account1.setNickName(account.getNickName());
        account1.setEmail(account.getEmail());
        return baseMapper.updateById(account1);
    }

    /**
     * 功能描述: <br>
     * 〈初始化用户对象〉
     *
     * @param:  account {@link UnAccount}
     * @since: 1.0.0
     * @auther: w10g
     * @return:
     * @Date: 2020/1/4 1:29
     */
    private void initUnAccount(UnAccount account){
        //初始化时间
        account.setCreateTime(new Date());
        account.setUpdateTime(new Date());
        account.setDelFlag(0);
        //初始化状态 初始化为禁用状态，除非你传值
        if(account.getStatus()==null){
            account.setStatus(1);
        }
        //密码加密
        account.setPassword(passwordEncoder.encode(account.getPassword()));
    }

}
